<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-select v-model="dataForm.tkId" filterable placeholder="请选择田块">
          <el-option value="">请选择田块</el-option>
            <el-option
            v-for="item in tkList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
            </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="time"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
          value-format="yyyy-MM-dd"
          >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
     
      <el-table-column
        prop="rtu"
        header-align="center"
        align="center"
        width="160"
        label="RTU序列号">
      </el-table-column>
      <el-table-column
        prop="baowentype"
        header-align="center"
        align="center"
        label="报文类型">
      </el-table-column>
      <el-table-column
        prop="tkName"
        header-align="center"
        align="center"
        width="160"
        label="所在田块">
      </el-table-column>
      <el-table-column
        prop="waterTemperature"
        header-align="center"
        align="center"
        width="100"
        label="水温">
      </el-table-column>
      <el-table-column
        prop="waterDepth"
        header-align="center"
        align="center"
        width="100"
        label="水深">
      </el-table-column>
      <el-table-column
        prop="windDirection"
        header-align="center"
        align="center"
        label="风向">
      </el-table-column>
       <el-table-column
        prop="fscgq"
        header-align="center"
        align="center"
        width="100"
        label="风速传感器">
      </el-table-column>
      <el-table-column
        prop="windVelocity"
        header-align="center"
        align="center"
        label="风速">
      </el-table-column>
      <el-table-column
        prop="gzd"
        header-align="center"
        align="center"
        label="光照度">
      </el-table-column>
      <el-table-column
        prop="ryl"
        header-align="center"
        align="center"
        width="100"
        label="日雨量">
      </el-table-column>
      <el-table-column
        prop="ljjy"
        header-align="center"
        align="center"
        width="100"
        label="累积雨量">
      </el-table-column>
      <el-table-column
        prop="gateA"
        header-align="center"
        align="center"
        label="闸门1">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.gateA == '关'" size="small" type="warning">关</el-tag>
          <el-tag v-else-if="scope.row.gateA == '开'" size="small" type="success">开</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="gateB"
        header-align="center"
        align="center"
        label="闸门2">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.gateB == '关'" size="small" type="warning">关</el-tag>
          <el-tag v-else-if="scope.row.gateB == '开'" size="small" type="success">开</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="gateC"
        header-align="center"
        align="center"
        label="闸门3">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.gateC == '关'" size="small" type="warning">关</el-tag>
          <el-tag v-else-if="scope.row.gateC == '开'" size="small" type="success">开</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="gateD"
        header-align="center"
        align="center"
        label="闸门4">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.gateD == '关'" size="small" type="warning">关</el-tag>
          <el-tag v-else-if="scope.row.gateD == '开'" size="small" type="success">开</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="gateE"
        header-align="center"
        align="center"
        label="闸门5">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.gateE == '关'" size="small" type="warning">关</el-tag>
          <el-tag v-else-if="scope.row.gateE == '开'" size="small" type="success">开</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="solarCell"
        header-align="center"
        align="center"
        label="电池">
      </el-table-column>
       <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        width="160"
        label="发报时间">
      </el-table-column>
       <el-table-column
        prop="updateTime"
        header-align="center"
        align="center"
        width="160"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="messageCode"
        header-align="center"
        align="center"
        label="原报文">
        <template slot-scope="scope">
           <el-popover
              placement="bottom"
              width="260"
              trigger="click"
              :content="scope.row.messageCode">
              <el-button size="mini" slot="reference">查看</el-button>
            </el-popover>
        </template>       
      </el-table-column>       
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './bussrturecord-add-or-update'
  export default {
    data () {
      return {
         pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        tkList:[],// 田块列表
        time:null
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
      this.getTkList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        var str=''
        if(this.time!=null&&this.time.length>1){
          var str1=this.time[0]
          var str2=this.time[1]
          str=str1+' - '+str2
        }    
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/buss/bussrturecord/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'name': this.dataForm.tkId,
            'time':str
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/buss/bussrturecord/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
       getTkList(){
          this.$http({
            url: this.$http.adornUrl('/buss/bussfarmlandinfo/list'),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
             this.tkList = data.page.list
          })    	     	       	
      },
    }
  }
</script>
